package com.dq.context.schedule;

import com.dq.domain.base.AbstractEntity;
import com.dq.domain.org.Org;
import com.dq.domain.record.MaintainRecord;
import com.dq.domain.record.ServiceRecord;
import com.dq.domain.setup.TrackGenerateRateSet;
import com.dq.domain.sys.user.user.base.SysUser;
import com.dq.domain.sys.user.user.WxUser;
import com.dq.service.AllService;
import com.dq.utils.TimeUtil;
import lombok.extern.java.Log;
import org.joda.time.DateTime;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashSet;
import java.util.List;

/**
 * @Author luchangtong
 * @Date 2019/12/9 10:41
 */
@Log
@Component
@EnableScheduling
public class TrackTask extends AllService implements SchedulingConfigurer {

    public static String cron ;
    @Override
    public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
//        //项目部署时，会在这里执行一次，从数据库拿到cron表达式
//            cron = "0 4 1 1 * ?";
//        TrackGenerateRateSet rateSet = trackGenerateRateSetService.get(TrackGenerateRateSet.SetStatus.generate);
//        if(rateSet != null){
//            cron=rateSet.getExpression();
//        }
//        Runnable task = new Runnable() {
//            @Override
//            public void run() {
//                //任务逻辑代码部分.
//                List<WxUser> wxUsers = wxUserRepository.findByDeletedFalseAndStaffUserNotNull();
//                HashSet<MaintainRecord> set = new HashSet<>();
//                HashSet<WxUser> wxUserHashSet = new HashSet<>();
//                wxUsers.forEach( wxUser -> {
//                    List<ServiceRecord> records = serviceRecordRepository.findByWxUserAndServiceStatusAndDeletedFalse((SysUser) wxUser, ServiceRecord.ServiceStatus.endService);
//                    if(records!=null && !records.isEmpty()){
//                        MaintainRecord t = new MaintainRecord();
//                        t.setTrackNo(AbstractEntity.generyStrAndMstime(""));
//                        t.setWxUser(wxUser);
//                        t.setSysDept((Org) wxUser.getSection());
//                        t.setMaintainStatus(MaintainRecord.MaintainStatus.noVisit);
//                        t.setServiceTime(records.get(0).getServiceBeginTime());
//                        t.setTimeoutTime(TimeUtil.formatFMT6Date(TimeUtil.dayAddFMT_6(DateTime.now(),rateSet.getDayNum().intValue()).toString()));
//                        set.add(t);
//                        wxUser.setGenerateTime(System.currentTimeMillis());
//                        wxUserHashSet.add(wxUser);
//                    }
//                });
//                maintainRecordRepository.saveAll(set);
//                wxUserRepository.saveAll(wxUserHashSet);
//
//
//            }
//        };
//        Trigger trigger = new Trigger() {
//            @Override
//            public Date nextExecutionTime(TriggerContext triggerContext) {
//                //任务触发，可修改任务的执行周期.
//                //每一次任务触发，都会执行这里的方法一次，重新获取下一次的执行时间
//                TrackGenerateRateSet rateSet = trackGenerateRateSetService.get(TrackGenerateRateSet.SetStatus.generate);
//                if(rateSet != null){
//                    cron=rateSet.getExpression();
//                }
//                CronTrigger trigger = new CronTrigger(cron);
//                Date nextExec = trigger.nextExecutionTime(triggerContext);
//                return nextExec;
//            }
//        };
//        scheduledTaskRegistrar.addTriggerTask(task, trigger);
    }

}
